﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DataClassLayer
{
    public class StatisticCollection
    {
        public StatisticCollection()
        {
            
        }
        public bool Add(Statistic statistic)
        {
            try
            {
                List<string> query = new List<string>();
                string command = @"Insert into SOCCER_STATISTICS (Win,Draw,Lose,ClubID,Year) Values
                   (" + statistic.Win + ","
                      + statistic.Draw + ","
                      + statistic.Lose + ","
                      + statistic.ClubID + ","
                   + statistic.Year + ")";
                query.Add(command);
                DataAccessObject.DataBase.CreateUpdateDelete(query);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool Edit(Statistic statistic)
        {
            try
            {
                List<string> query = new List<string>();
                string command = @"Update SOCCER_STATISTICS Set 
                      Win=" + statistic.Win +
                     ",Draw=" + statistic.Draw +
                      ",Lose=" + statistic.Lose +
                      ",ClubID=" + statistic.ClubID +
                     ",Year=" + statistic.Year + " Where ID=" + statistic.ID;
                query.Add(command);
                DataAccessObject.DataBase.CreateUpdateDelete(query);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool Delete(Statistic statistic)
        {
            try
            {
                List<string> query = new List<string>();
                string command = @"Delete From SOCCER_STATISTICS Where ID= " + statistic.ID; 
                query.Add(command);
                DataAccessObject.DataBase.CreateUpdateDelete(query);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public DataTable FillList(Statistic statistic)
        {
            try
            {
                string wherestatement = WhereStatement(statistic);
                string command = @"SELECT        SOCCER_STATISTICS.ID, SOCCER_STATISTICS.Win, SOCCER_STATISTICS.Draw, SOCCER_STATISTICS.Lose, SOCCER_STATISTICS.ClubID, 
                         SOCCER_STATISTICS.Year, SOCCER_CLUB_INFO.Name AS ClubName
                         FROM            SOCCER_STATISTICS INNER JOIN
                         SOCCER_CLUB_INFO ON SOCCER_STATISTICS.ClubID = SOCCER_CLUB_INFO.ID";
                if (wherestatement != null &&
                    wherestatement != "")
                {
                    command += " Where " + wherestatement;
                }
                DataTable table = DataAccessObject.DataBase.ExcuteQuery(command);
                return table;
            }
            catch
            {
                return null;
            }
        }
        public int MaxID()
        {
            try
            {
                int Maxid = 1;
                string command = @"SELECT    Max(ID) AS MaxID FROM   SOCCER_STATISTICS";
                DataTable table = DataAccessObject.DataBase.ExcuteQuery(command);
                if (table.Rows.Count == 1)
                {
                    Maxid = int.Parse(table.Rows[0]["MaxID"].ToString());
                }
                return Maxid;
            }
            catch
            {
                return 1;
            }
        }
        public string WhereStatement(Statistic statistic)
        {
            string wherestatement = "";
            if (statistic.ID > 0)
            {
                if (wherestatement != null &&
                    wherestatement != "")
                {
                    wherestatement += "And SOCCER_STATISTICS.ID= " + statistic.ID;
                }
                else
                {
                    wherestatement += "SOCCER_STATISTICS.ID= " + statistic.ID;
                }
            }
            if (statistic.Year > 0)
            {
                if (wherestatement != null &&
                    wherestatement != "")
                {
                    wherestatement += "And SOCCER_STATISTICS.Year= " + statistic.Year;
                }
                else
                {
                    wherestatement += "SOCCER_STATISTICS.Year= " + statistic.Year;
                }
            }
            if (statistic.ClubID > 0)
            {
                if (wherestatement != null &&
                    wherestatement != "")
                {
                    wherestatement += "And SOCCER_STATISTICS.ClubID= " + statistic.ClubID;
                }
                else
                {
                    wherestatement += "SOCCER_STATISTICS.ClubID= " + statistic.ClubID;
                }
            }
            return wherestatement;
        }
    }
}
